layui.extend({
    zmnTable: "zmn.table",
    // oms_work_table_config: 'work/zmn.oms.work.table.config',

}).define(['zmnTable','form'],function (exports) {
    let $ = layui.$;
    let form = layui.form;
    let zmnTable = layui.zmnTable;
    let cols = [
        {field:'callTime',title:'通话时间'},
        {field:'phone',title:'用户来电'},
        {field:'cityName',title:'城市'},
        {field:'callInOut',title:'呼入呼出'},
        {field:'operator',title:'话务员'},
        {field:'callType',title:'通话类型'},
        {field:'channelName',title:'来源'},
        {field:'soundSrc',title:'录音', templet:'#play-btn'},
    ];
    function initPage() {
        if(loadUserPhone){
            searchForTable({phone:loadUserPhone});
        }

    }
    function initEvent(){
        form.on('submit(submit-search)',function (data){
            let phone = data.field;
            searchForTable(phone);
            return false;
        });
        $('.layui-card-body').on('click','.play-btn',function (e){
            let src = e.currentTarget.dataset.src;
            playRecord(src);
        })
    }
    function playRecord(src){
        layer.open({
            title: '录音详情',
            shadeClose:true,
            content:`<audio src="${src}" controls autoplay></audio>`
        });
    }
    
    function init(){
        initPage();
        initEvent();
    }
    
    $(function (){
        init();
    });

    /**
     * 搜索框搜索方法
     * @param {object} params 查询参数
     */
    function searchForTable (params, data) {
        renderTable(params, data);
    }
    /**
     * 渲染表格
     * @param {object} params 查询参数
     * @returns {object}
     */
    function renderTable (params, data) {
        return zmnTable.init({
            config: {
                url:'/call/record/ajax/list.action?',
                cols:[cols],
                elem: '#zmn-record-table',
                method: 'GET',
                where: {
                    phone: params.phone
                },
                limit: 100,
                limits: [100],
                data: data,
                parseData: function (res) {
                    return {
                        "code": res.status, //解析接口状态
                        "msg": res.message, //解析提示文本
                        "count": res.data ? (res.data.count > 10000 ? 10000 : res.data.count) : 0, //解析数据长度
                        "data": res.data ? res.data.data : [] //解析数据列表
                    };
                },
                done: function (res, curr, count) {
                    count = count || 0;
                    $('.layui-this').find('.count').text('（共 ' + count + ' 条）');
                }
            }
        });
    }

    exports('oms_recordData');
});